package com.wmm.controller;

import com.wmm.domain.ResourceCategory;
import com.wmm.domain.ResponseResult;
import com.wmm.service.ResourceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/ResourceCategory")
public class ResourceCategoryController {

    @Autowired
    private ResourceCategoryService resourceCategoryService;

   /*
        名称: findAllResourceCategory
        描述: 查询资源分类信息
        URL: http://localhost:8080/ssm_web/ResourceCategory/findAllResourceCategory
        请求方式: GET
    */
    @RequestMapping("/findAllResourceCategory")
    public ResponseResult findAllResourceCategory() {
        List<ResourceCategory> allResourceCategory = resourceCategoryService.findAllResourceCategory();
        return new ResponseResult(true,200,"查询所有分类信息成功",allResourceCategory);
    }


    /*
        添加 or 修改资源分类
        名称: saveOrUpdateResourceCategory
        描述: 添加 or 修改资源分类接口
        URL: http://localhost:8080/ssm_web/ResourceCategory/saveOrUpdateResourceCategory
        请求方式: POST
     */
    @RequestMapping("/saveOrUpdateResourceCategory")
    public ResponseResult saveOrUpdateResourceCategory(@RequestBody ResourceCategory resourceCategory) {
        Map<String,Object> map = new HashMap<>();
        map.put("name",resourceCategory.getName());
        map.put("sort",resourceCategory.getSort());
        ResponseResult responseResult = null;
        //新增操作
        if(null == resourceCategory.getId()) {
            resourceCategoryService.saveResourceCategory(resourceCategory);
            responseResult = new ResponseResult(true,200,"新增资源分类成功",map);

        }else {   //修改操作
            resourceCategoryService.updateResourceCategory(resourceCategory);
            responseResult = new ResponseResult(true,200,"修改资源分类成功",map);
        }
        return responseResult;
    }


    /*
        名称: deleteResourceCategory
        描述: 删除资源分类接口
        URL: http://localhost:8080/ssm_web/ResourceCategory/deleteResourceCategory
        请求方式: GET
     */
    @RequestMapping("/deleteResourceCategory")
    public ResponseResult deleteResourceCategory(int id) {
        resourceCategoryService.deleteResourceCategory(id);
        return new ResponseResult(true,200,"删除资源分类成功",null);
    }


}

